package com.wms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.bean.Transfer;
import com.wms.config.Result;
import com.wms.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/transfer")
@CrossOrigin
public class TransferController {
    @Autowired
    TransferService transferService;
    @GetMapping("/list")
  public Result list() {
        List<Transfer> transfers = transferService.list();
        return Result.success(transfers);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Transfer transfer) {
        boolean isSaved = transferService.save(transfer);
        if (isSaved) {
            return Result.success("转移单添加成功", transfer);
        } else {
            return Result.fail(201, "转移单添加失败");
        }
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean isDeleted = transferService.removeById(id);
        if (isDeleted) {
            return Result.success("转移单删除成功");
        }
        else {
            return Result.fail(201, "转移单删除失败");
        }
    }
    @PutMapping("/update")
    public Result update(@RequestBody Transfer transfer) {
        boolean isUpdated = transferService.updateById(transfer);
        if (isUpdated) {
            return Result.success("转移单更新成功", transfer);
        } else {
            return Result.fail(201, "转移单更新失败");
        }
    }
    @GetMapping("page")
    public Result getPage(Page page){
        Page result = transferService.page(page);
        return Result.success(result);
    }
}
